package com.plian.system.controller.pm.PropertyInfor;

import com.alibaba.fastjson.JSONObject;
import com.plian.Tools.EncryptUtil;
import com.plian.Tools.StringUtil;
import com.plian.system.entity.pm.propertyinformation.*;
import com.plian.system.mapper.pm.propertyInfo.PropertyInfoMapper;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huzb
 * @create 2020-10-13 11:02
 */
@RestController
@RequestMapping(value = "api/propertyinfo")
public class PropertyInforController {

    @Resource
    PropertyInfoMapper propertyInfoMapper;

    /**
     * 第一次查询最新表
     * @param json
     * @return
     */
    @PostMapping(value = "/selectAll")
//    @PassToken
    public List<PropertyInfoOut> selectAll(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String token = jsonObject.getString("token");
        String appId = jsonObject.getString("appId");
        String key = jsonObject.getString("key");
        String timestamp = jsonObject.getString("timestamp");
        String str = key + appId + timestamp;
        //token解密
        token = EncryptUtil.decryptBase64(token, key);
        if (StringUtil.isBlank(token)){
            System.out.println("token为空");
        }else if (!token.equals(DigestUtils.md5DigestAsHex(str.getBytes()))){
            System.out.println("token无效");
        }else {
            return  propertyInfoMapper.selectFirst();
        }
        return null;
    }

    /**
     * 第二次查询时间段内更新的
     * @param json
     * @return
     */
    @PostMapping(value = "/selectByTime")
//    @PassToken
    public List<PropertyInformation> selectByTime(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String token = jsonObject.getString("token");
        String appId = jsonObject.getString("appId");
        String key = jsonObject.getString("key");
        String timestamp = jsonObject.getString("timestamp");
        String starttime = jsonObject.getString("starttime");
        String endtime = jsonObject.getString("endtime");
        PropertyTime time = new  PropertyTime(starttime,endtime);
        String str = key + appId + timestamp;
        //token解密
        token = EncryptUtil.decryptBase64(token, key);
        if (StringUtil.isBlank(token)){
            System.out.println("token为空");
        }else if (!token.equals(DigestUtils.md5DigestAsHex(str.getBytes()))){
            System.out.println("token无效");
        }else {
            return  propertyInfoMapper.selectByTime(time);
        }
        return null;
    }


    /**
     * 第一次查询股份人员
     * @param json
     * @return
     */
    @PostMapping(value = "/select-equity")
//    @PassToken
    public List<EquityStruct> selectEquityStruct(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String token = jsonObject.getString("token");
        String appId = jsonObject.getString("appId");
        String key = jsonObject.getString("key");
        String timestamp = jsonObject.getString("timestamp");
        String str = key + appId + timestamp;
        //token解密
        token = EncryptUtil.decryptBase64(token, key);
        if (StringUtil.isBlank(token)){
            System.out.println("token为空");
        }else if (!token.equals(DigestUtils.md5DigestAsHex(str.getBytes()))){
            System.out.println("token无效");
        }else {
            return  propertyInfoMapper.selectEquityStruct();
        }
         return null;
    }

    /**
     * 第一次查询参股人员
     * @param json
     * @return
     */
    @PostMapping(value = "/stack-equity")
//    @PassToken
    public List<StackEquity> selectStackEquity(@RequestBody String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String token = jsonObject.getString("token");
        String appId = jsonObject.getString("appId");
        String key = jsonObject.getString("key");
        String timestamp = jsonObject.getString("timestamp");
        String str = key + appId + timestamp;
        //token解密
        token = EncryptUtil.decryptBase64(token, key);
        if (StringUtil.isBlank(token)){
            System.out.println("token为空");
        }else if (!token.equals(DigestUtils.md5DigestAsHex(str.getBytes()))){
            System.out.println("token无效");
        }else {
            return  propertyInfoMapper.selectStackEquity();
        }
        return null;
    }

    @PostMapping(value = "/selectTest")
//    @PassToken
    public void selectTest(@RequestBody String json){

    }
}
